package field

import "gitee.com/yanwc/net/utils"

type (
	Speed    float64
	SpeedRaw [2]byte // 速度 单位: 0.1km/h
)

const (
	SpeedUnit  float64 = 0.1 // 0.1km/h
	SpeedRatio float64 = 10
)

func (s Speed) Raw() (out SpeedRaw) {
	v := uint16(float64(s) * SpeedRatio)
	copy(out[:], utils.NumberToBytesWithBigEndian(v))
	return
}

func (s SpeedRaw) Type() Speed {
	f := float64(utils.BytesToNumber[uint16](s[:])) * SpeedUnit
	return Speed(utils.Decimal(f, 1))
}

func (s SpeedRaw) Slice() []byte {
	return s[:]
}
